package com.intellagric.service.agritech.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.common.utils.UUIDUtils;
import com.intellagric.dao.agritech.AgtAgritechDao;
import com.intellagric.mapper.AgtAgritechMapper;
import com.intellagric.mapper.AgtResourceMapper;
import com.intellagric.module.agritech.AgtAgritechVo;
import com.intellagric.pojo.AgtAgritech;
import com.intellagric.pojo.AgtAgritechExample;
import com.intellagric.pojo.AgtResource;
import com.intellagric.service.agritech.AgritechService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: cwq
 * @Date: 2019/2/22 19:30
 * @Description:
 */
@Service
public class AgritechServiceImpl implements AgritechService {

    @Autowired
    private AgtAgritechMapper agtAgritechMapper;

    @Autowired
    private AgtAgritechDao agtAgritechDao;

    @Autowired
    private AgtResourceMapper agtResourceMapper;

    @Override
    public ResponseMessage addAgritech(AgtAgritech agritech) {
        AgtResource agtResource = new AgtResource();
        Date createdTime = new Date();
        String resourceId = UUIDUtils.getID();
        String agritechId = UUIDUtils.getID();

        agtResource.setId(resourceId);
        agtResource.setFid(agritechId);
        agtResource.setResourceTypeId("da75101e05da4eb4a5f94b0cb0f1324a");
        agtResource.setCreatedTime(createdTime);

        agritech.setId(agritechId);
        agritech.setResourceId(resourceId);
        agritech.setCreatedTime(createdTime);

        return agtResourceMapper.insertSelective(agtResource) == 1 ? (
                agtAgritechMapper.insertSelective(agritech) == 1 ? ResponseMessage.success().add("add", agritech) : ResponseMessage.fail()
        ) : ResponseMessage.fail();
    }

    @Override
    public ResponseMessage deleteAgritech(String id) {
        return agtAgritechMapper.deleteByPrimaryKey(id) == 1 ? ResponseMessage.success() : ResponseMessage.fail();
    }

    @Override
    public ResponseMessage editAgritech(AgtAgritech agritech) {
        return agtAgritechMapper.updateByPrimaryKeySelective(agritech) == 1 ? ResponseMessage.success().add("update", agritech) : ResponseMessage.fail();
    }

    @Override
    public AgtAgritech getAgritechById(String id) {
        return agtAgritechMapper.selectByPrimaryKey(id);
    }

    @Override
    public LayuiDataGridResult getAgritechList() {
        AgtAgritechExample agtAgritechExample = new AgtAgritechExample();
        agtAgritechExample.setOrderByClause("created_time ASC");
        agtAgritechExample.createCriteria().andIdIsNotNull();
        List<AgtAgritech> agtAgritechList = agtAgritechMapper.selectByExample(agtAgritechExample);
        return LayuiDataGridResult.success().add(agtAgritechList, agtAgritechList.size());
    }

    @Override
    public LayuiDataGridResult getAgritechByPage(int page, int limit) {
        PageHelper.startPage(page, limit, "created_time ASC");
        AgtAgritechExample agritechExample = new AgtAgritechExample();
        agritechExample.createCriteria().andIdIsNotNull();
        List<AgtAgritech> agritechList = agtAgritechMapper.selectByExample(agritechExample);
        return LayuiDataGridResult.success().add(agritechList, this.getAgritechList().getCount());
    }

    @Override
    public LayuiDataGridResult getAgritechListByVo(AgtAgritechVo agtAgritechVo, int page, int limit) {
        PageHelper.startPage(page, limit, "created_time DESC");
        AgtAgritechExample agtAgritechExample = new AgtAgritechExample();
//        agtAgritechExample.createCriteria().andIdIsNotNull().andSpeciesIdEqualTo(agtAgritechVo.getSpeciesId());
        List<AgtAgritech> agritechList = agtAgritechDao.selectAgritechListByVo(agtAgritechVo);
//        List<AgtAgritech> agritechList = new PageInfo<>(agtAgritechMapper.selectByExample(agtAgritechExample)).getList();
        return LayuiDataGridResult.success().add(agritechList, this.getAgritechList().getCount());
//        return LayuiDataGridResult.success().add(agritechList, agritechList.size());
    }
}
